
#include <qlabel.h>
#include <qlayout.h>
#include <qfiledialog.h>


#include "BnetLoggerSettingsWidget.h"


BnetLoggerSettingsWidget::BnetLoggerSettingsWidget( QWidget * parent, 
						    const char * name, 
						    WFlags f )
    : 
    QWidget( parent, name, f ),
    WIDGET_SPACING_SIZE( 15 )
{
    // Widget creation.
    enabledCheckBox_ = new QCheckBox( tr("&Enabled"), this );
    logUserInChannelCheckBox_ = new QCheckBox( tr("Log users in a channel") +
					       tr(" when joining channel"), 
					       this );
    logUserJoinLeaveCheckBox_ = new QCheckBox( tr("Log users joining and ") +
					       tr("leaving channel"), this );
    QLabel *filenameLabel = new QLabel( tr("&Log file: "), this );
    filenameLineEdit_ = new QLineEdit( this );
    browseButton_ = new QPushButton( tr("&Browse"), this );
    
    // Set the buddy.
    filenameLabel->setBuddy( filenameLineEdit_ );
    
    // Set up the layouts.
    QVBoxLayout *topLayout = new QVBoxLayout;
    topLayout->addWidget( enabledCheckBox_ );
    topLayout->addSpacing( WIDGET_SPACING_SIZE );
    topLayout->addStretch();
    topLayout->addSpacing( WIDGET_SPACING_SIZE );
    topLayout->addWidget( logUserInChannelCheckBox_ );
    topLayout->addSpacing( WIDGET_SPACING_SIZE );
    topLayout->addWidget( logUserJoinLeaveCheckBox_ );
    
    QHBoxLayout *bottomLayout = new QHBoxLayout;
    bottomLayout->addWidget( filenameLabel );
    bottomLayout->addWidget( filenameLineEdit_ );
    bottomLayout->addWidget( browseButton_ );
    
    QVBoxLayout *mainLayout = new QVBoxLayout( this );
    mainLayout->addSpacing( WIDGET_SPACING_SIZE );
    mainLayout->addLayout( topLayout );
    mainLayout->addSpacing( WIDGET_SPACING_SIZE );
    mainLayout->addLayout( bottomLayout );
    mainLayout->addSpacing( WIDGET_SPACING_SIZE );
    
    // Connect signals to slots.
    connect( enabledCheckBox_, SIGNAL(toggled(bool)),
	     this, SLOT(enableLogger(bool)) );
    connect( browseButton_, SIGNAL(clicked()),
	     this, SLOT(browseButtonClicked()) );
    
    // Default the logger to be disabled.
    this->setEnabled( false );
}


void BnetLoggerSettingsWidget::setEnabled( const bool& enabled )
{
    enabledCheckBox_->setChecked( enabled );
    this->enableLogger( enabled );
}

void BnetLoggerSettingsWidget::setLogUserInChannel( const bool& enabled )
{
    logUserInChannelCheckBox_->setChecked( enabled );
}

void BnetLoggerSettingsWidget::setLogUserJoinLeave( const bool& enabled )
{
    logUserJoinLeaveCheckBox_->setChecked( enabled );
}


void BnetLoggerSettingsWidget::setFilename( const QString& filename )
{
    filenameLineEdit_->setText( filename );
}

bool BnetLoggerSettingsWidget::getEnabled() const
{
    return enabledCheckBox_->isChecked();
}

bool BnetLoggerSettingsWidget::getLogUserInChannel() const
{
    return logUserInChannelCheckBox_->isChecked();
}

bool BnetLoggerSettingsWidget::getLogUserJoinLeave() const
{
    return logUserJoinLeaveCheckBox_->isChecked();
}

QString BnetLoggerSettingsWidget::getFilename() const
{
    return filenameLineEdit_->text();
}


void BnetLoggerSettingsWidget::enableLogger( bool enabled )
{
    logUserInChannelCheckBox_->setEnabled( enabled );
    logUserJoinLeaveCheckBox_->setEnabled( enabled );
    filenameLineEdit_->setEnabled( enabled );
    browseButton_->setEnabled( enabled );
}

void BnetLoggerSettingsWidget::browseButtonClicked()
{
    QFileDialog* fd = new QFileDialog( this );
    fd->setMode( QFileDialog::AnyFile );
    fd->addFilter("Text Files (*.txt *.TXT)");

    if( fd->exec() == QDialog::Accepted )
    {
	filenameLineEdit_->setText( fd->selectedFile() );
    }
}



// End of file.
